/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.stripes.hyperstripes.core;

import com.farent.solutions.plugins.stripes.hyperstripes.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author farouka
 */
public abstract class DefaultComponentModel implements ComponentModel {

    private String text;
    private String name;
    private String id;
    private String componentId;
    private List<Attribute> attributes;
    private List<ComponentModel> children;
    private ComponentModel parent;
    private DefaultComponentTypeStateHelper modelState;

    public DefaultComponentModel() {
        attributes = new ArrayList<Attribute>();
        children = new ArrayList<ComponentModel>();
    }


    @Override
    public void processTag(XMLStreamReader streamReader) {
        String idAttr = null;
        setName(streamReader.getName().getPrefix() + ":" + streamReader.getName().getLocalPart());
        for (int i = 0, n = streamReader.getAttributeCount(); i < n; ++i) {
            Attribute componentAttr = new Attribute();
            QName an = streamReader.getAttributeName(i);
            String av = streamReader.getAttributeValue(i);
            av = av.trim();
            if ( !Utils.assignIfhasOwnProperty( this, an.getLocalPart(), av) ) {
                componentAttr.setName(an.getLocalPart());
                componentAttr.setValue(av);
                addAttribute(componentAttr);
                if (an.getLocalPart().equalsIgnoreCase("id")) {
                    idAttr = av;
                }
            }
        }
        String uniqueId = createUniqueId(idAttr);
        addAttribute(new Attribute("id", uniqueId));
        componentId = createComponentId(this);
        getComponentTypeStateHelper().saveComponentKeyMap(uniqueId, componentId);
//        addAttribute(new Attribute("componentId", componentId ));
    }

    @Override
    public String createUniqueId(String seed) {
        String tempId = seed;
        if (getComponentTypeStateHelper() != null) {
            tempId = getComponentTypeStateHelper().createUniqueId(seed);
        }
        setId(tempId);
        return tempId;
    }

    @Override
    public String createComponentId(ComponentModel model) {
        String tempId = Utils.EMPTY_STRING;
        if (getComponentTypeStateHelper() != null) {
            tempId = getComponentTypeStateHelper().createComponentId(model);
        }
        model.setComponentId(tempId);
        return tempId;
    }

    @Override
    public void setComponentTypeStateHelper(ComponentTypeStateHelper modelState) {
        this.modelState = (DefaultComponentTypeStateHelper) modelState;
    }

    @Override
    public ComponentTypeStateHelper getComponentTypeStateHelper() {
        return modelState;
    }

    @Override
    public ComponentModel getFirstChild() {
        if (children != null && !children.isEmpty()) {
            return children.get(0);
        }

        return null;
    }

    @Override
    public ComponentModel getChild( String childId ) {
        if( childId == null || childId.isEmpty() )
            return null;
        for (int i = 0; i < children.size(); i++) {
            ComponentModel child = children.get(i);
            if( child != null && child.getId().equals(childId))
                return child;
        }
        return null;
    }

    @Override
    public Attribute getAttribute( String attributeName ) {
        if( attributeName == null || attributeName.isEmpty() )
            return null;
        for (int i = 0; i < attributes.size(); i++) {
            Attribute child = attributes.get(i);
            if( child != null && child.getName().equals(attributeName))
                return child;
        }
        return null;
    }

    @Override
    public Attribute getFirstAttribute() {
        if (attributes != null && !attributes.isEmpty()) {
            return attributes.get(0);
        }
        return null;
    }

    @Override
    public ComponentModel getLastChild() {
        if (children != null && !children.isEmpty()) {
            int size = children.size();
            return children.get(size - 1);
        }
        return null;
    }

    @Override
    public Attribute getLastAttribute() {
        if (attributes != null && !attributes.isEmpty()) {
            int size = attributes.size();
            return attributes.get(size - 1);
        }
        return null;
    }

    @Override
    public void addAttribute(Attribute attribute) {
        attributes.add(attribute);
    }

    @Override
    public void AddChild(ComponentModel child) {
        children.add(child);
    }

    @Override
    public void setText(String text) {
        this.text = text;
    }

    @Override
    public List<Attribute> getAttributes() {
        return attributes;
    }

    @Override
    public List<ComponentModel> getChildren() {
        return children;
    }

    @Override
    public String getText() {
        return text;
    }

    @Override
    public abstract String getName();

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String getComponentId() {
        return componentId;
    }

    @Override
    public void setComponentId(String componentId) {
        this.componentId = componentId;
    }

    @Override
    public ComponentModel getParent() {
        return parent;
    }

    @Override
    public void setParent(ComponentModel parent) {
        this.parent = parent;
    }

    @Override
    public boolean isChild(ComponentModel child) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
